package com.lmqb.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmqb.bean.LmCoupon;
import com.lmqb.bean.LmUser;
import com.lmqb.bean.LmUserInfo;
import com.lmqb.bean.LogEmail;
import com.lmqb.bean.SysArea;
import com.lmqb.bean.SysBlack;
import com.lmqb.bean.SysSchool;
import com.lmqb.dao.LmCouponMapper;
import com.lmqb.dao.LmUserInfoMapper;
import com.lmqb.dao.LmUserMapper;
import com.lmqb.dao.SysAreaMapper;
import com.lmqb.dao.SysBlackMapper;
import com.lmqb.dao.SysSchoolMapper;
import com.lmqb.dao.SysUserMapper;
import com.lmqb.page.AreaPager;
import com.lmqb.page.CouponPager;
import com.lmqb.page.PageTool;
import com.lmqb.page.SchoolPager;
import com.lmqb.page.SysBlackPager;
import com.lmqb.service.DataService;
import com.lmqb.util.StrUtil;
@Service("dataService")
public class DataServiceImpl implements DataService{

	@Autowired
	private SysBlackMapper sysBlackDao;
	
	@Autowired
	private SysSchoolMapper schoolDao;
	
	@Autowired
	private SysAreaMapper areaDao;
	
	@Autowired
	private LmCouponMapper couponDao;
	
	@Autowired
	private LmUserMapper lmUserDao;
	
	@Autowired
	private LmUserInfoMapper lmUserInfoDao;
	
	/**
	 * @show 黑名单列表
	 * 
	 * @param SysBlackPager
	 * 
	 * @param SysBlackPager
	 * */
	public SysBlackPager selectBlackPager(SysBlackPager pagerModel) {
		List<SysBlack> list=sysBlackDao.selectAll(pagerModel);
		for(int i=0;i<list.size();i++){
			list.get(i).setCreateTimeD(StrUtil.intToTime(list.get(i).getCreateTime()));
		}
		pagerModel.setList(list);
		int count = sysBlackDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	/**
	 * @show  学校列表
	 * 
	 * @param SchoolPager
	 * 
	 * @param SchoolPager
	 * */
	public SchoolPager selectSchoolPager(SchoolPager pagerModel) {
		List<SysSchool> list=schoolDao.selectAll(pagerModel);
		pagerModel.setList(list);
		int count = schoolDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show  地区列表
	 * 
	 * @param AreaPager
	 * 
	 * @param AreaPager
	 * */	
	public AreaPager selectAreaPager(AreaPager pagerModel) {
		List<SysArea> list=areaDao.selectAll(pagerModel);
		pagerModel.setList(list);
		int count = areaDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show  优惠券
	 * 
	 * @param AreaPager
	 * 
	 * @param AreaPager
	 * */		
	public CouponPager selectCouponPager(CouponPager pagerModel) {
		List<LmCoupon> list=couponDao.selectAll(pagerModel);
//		List<LmUser> userList=lmUserDao.selectAllUser();
		List<LmUserInfo> userInfos=lmUserInfoDao.selectAll();
		for(int i=0;i<list.size();i++){
			list.get(i).setExpireTimeD(StrUtil.intToTime(list.get(i).getExpireTime()));
			list.get(i).setObtainTimeD(StrUtil.intToTime(list.get(i).getObtainTime()));
			list.get(i).setUseTimeD(StrUtil.intToTime(list.get(i).getUseTime()));
//			for(int j=0;j<userList.size();j++){
//				if(list.get(i).getUserId()==userList.get(j).getUserId()){
//					list.get(i).setMobile(userList.get(j).getLoginMobile());
//					break;
//				}
//			}
			for(int k=0;k<userInfos.size();k++){
				if(list.get(i).getUserId().intValue()==userInfos.get(k).getUserId().intValue()){
					list.get(i).setUserName(userInfos.get(k).getUserName());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = couponDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	//启用省市区
	public int updateStatusByarea(String areaNo) {
		
		return areaDao.updateStatusByarea(areaNo);
	}
	//禁用省市区
	public int updateStatusByarea1(String areaNo) {
		
		return areaDao.updateStatusByarea1(areaNo);
	}

	@Override
	public void updateArea(String address){
		String[] split = address.split("/");
		String areaName=null;
		if(split.length==1){
			areaName=split[0];
		SysArea sysArea=areaDao.selectByAddress(areaName);
		String areaNo=sysArea.getAreaNo();
		String no = areaNo.substring(0, 2);
		areaDao.updateNo(no);
	
		
		
			
			
		}
		if(split.length==2){
			areaName=split[1];
			SysArea sysArea=areaDao.selectByAddress(areaName);
			String areaNo=sysArea.getAreaNo();
			String no = areaNo.substring(0, 4);
            areaDao.updateNo(no);
			
		}
	
	}
	


}
